/*
The MIT License

Copyright (c) 2010 Guillaume Florimond (www.valhalla.fr)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
 */

/*
 * ToolsFrame.java
 *
 * Created on 18 mars 2010, 18:02:51
 */

package fr.valhalla.vcrypt.ui;

import fr.valhalla.vcrypt.cyphers.ASCII;
import fr.valhalla.vcrypt.cyphers.Cypher;
import fr.valhalla.vcrypt.cyphers.Romanizer;
import fr.valhalla.vcrypt.cyphers.Rot13;

/**
 * ToolsFrame
 * The GUI main frame, with "crypt" and "decrypt" buttons
 * This class must be instanciated from the Main class. It mades itself visible.
 * @author Guillaume Florimond < gf@valhalla.fr > < www.valhalla.fr >
 */
public class ToolsFrame extends javax.swing.JFrame {

    MessageFrame mfIn, mfOut;


    /** Creates new form ToolsFrame */
    public ToolsFrame() {
        initComponents();

        this.setVisible(true);

        int xlimit = this.getX() + this.getWidth();

        mfIn = new MessageFrame();
        mfIn.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        mfIn.setLocation(xlimit, mfIn.getY());
        mfIn.setLabelInitial();
        mfIn.setVisible(true);
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        mainPane = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        cypherCombo = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        cRadio = new javax.swing.JRadioButton();
        dRadio = new javax.swing.JRadioButton();
        goButton = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel2.setText("Algorithme :");

        cypherCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Romanizer", "Rot13", "ASCII" }));

        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel3.setText("Sens : ");

        buttonGroup1.add(cRadio);
        cRadio.setSelected(true);
        cRadio.setText("Chiffrement");

        buttonGroup1.add(dRadio);
        dRadio.setText("Déchiffrement");

        goButton.setText("Go!");
        goButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                goButtonActionPerformed(evt);
            }
        });

        jPanel1.setBackground(new java.awt.Color(102, 153, 255));

        jLabel1.setBackground(new java.awt.Color(102, 153, 255));
        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18));
        jLabel1.setForeground(new java.awt.Color(255, 255, 255));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("VCrypt 0.1");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 238, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 45, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout mainPaneLayout = new javax.swing.GroupLayout(mainPane);
        mainPane.setLayout(mainPaneLayout);
        mainPaneLayout.setHorizontalGroup(
            mainPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(mainPaneLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mainPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(18, 18, 18)
                .addGroup(mainPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(dRadio)
                    .addComponent(cypherCombo, 0, 122, Short.MAX_VALUE)
                    .addComponent(cRadio)
                    .addComponent(goButton, javax.swing.GroupLayout.DEFAULT_SIZE, 122, Short.MAX_VALUE))
                .addGap(14, 14, 14))
        );
        mainPaneLayout.setVerticalGroup(
            mainPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPaneLayout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(mainPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cypherCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(31, 31, 31)
                .addGroup(mainPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cRadio)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(dRadio)
                .addGap(18, 18, 18)
                .addComponent(goButton)
                .addContainerGap(84, Short.MAX_VALUE))
        );

        getContentPane().add(mainPane, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void goButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_goButtonActionPerformed
        
        /* Crée un objet correspondant à l'algo choisi par l'utilisateur */
        int cs = cypherCombo.getSelectedIndex();
        System.out.println("Cypher selected : " + cs);

        Cypher c;

        switch(cs){
            default: c = new Rot13(); break;
            case 0 : c = new Romanizer(); break;
            case 1 : c = new Rot13(); break;
            case 2 : c = new ASCII(); break;
        }

        /* Détermine l'action à mener : toC = chiffrer ; toD = déchiffrer */
        boolean toC, toD;
        if(cRadio.isSelected()) { toC = true; toD = false; }
        else { toC = false; toD = true; }

        /* Chiffre/déchiffre */
        String in = mfIn.getMessage();
        c.setInput(in);
        if(toC) c.crypt(); else c.decrypt();
        String out = c.getOutput();

        /* Affichage du résultat */
        mfOut = new MessageFrame();
        mfOut.setLocation(mfIn.getX() + mfIn.getWidth(), mfIn.getY());
        if(toC) mfOut.setStatusCrypted(); else mfOut.setStatusDecrypted();
        mfOut.setVisible(true);
        mfOut.setTextEditable(false);
        mfOut.setMessage(out);
    }//GEN-LAST:event_goButtonActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ToolsFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JRadioButton cRadio;
    private javax.swing.JComboBox cypherCombo;
    private javax.swing.JRadioButton dRadio;
    private javax.swing.JButton goButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel mainPane;
    // End of variables declaration//GEN-END:variables

}
